package com.cloudbroker.bcs.platform.ltb.middle.util;

import com.cloudbroker.bcs.common.util.RandomUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.VerifySource;
import com.cloudbroker.bcs.platform.ltb.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.ltb.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.ltb.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.ltb.middle.model.VerifyCodeCache;

/**
 * Created by tianrui on 2016/7/9.
 */
public class VerifyCodeTokenUtil {


    private static final int SecondToMilliSecond = 1000;


    /**
     * 获取短信缓存key
     * verify_key = login_account + company_id + sendercomp_id
     * */
    public static String getVerifyCodeToken(String verify_key, VerifySource verify_source){
        SessionHelper sessionHelper = SessionHelper.getInstance();
        String verify_token = sessionHelper.getSendVerifyKey(verify_key,
                ValueUtil.getString(verify_source.getVal()));
        return verify_token;
    }


    /**
     * 增加短信缓存
     * 生成verify_code
     * */
    public static String add( String verify_code,String verify_token){
//        String verify_code = getVerifyCode();
        VerifyCodeCache verifyCodeCache = new VerifyCodeCache();
        verifyCodeCache.setVerify_code(verify_code);
        verifyCodeCache.setCreate_time(System.currentTimeMillis());
        verifyCodeCache.setVerify_code_count(0);
        updateVerifyCodeCache(verify_token, verifyCodeCache);
        return verify_code;
    }

    /**
     * 获取短信缓存对象
     * */
    public static VerifyCodeCache getVerifyCodeCache(String verify_token){
        CacheHelper cacheHelper = CacheHelper.getInstance();
        VerifyCodeCache verifyCodeCache = VerifyCodeCache.fromStringInfo(cacheHelper.get(verify_token));
        return verifyCodeCache;
    }


    /**
     * 更新短信缓存
     * */
    public static String update(String verify_code,String verify_token){
//        String verify_code = getVerifyCode();
        VerifyCodeCache verifyCodeCache = getVerifyCodeCache(verify_token);
        if (verifyCodeCache == null) return "";
        verifyCodeCache.setVerify_code(verify_code);
        verifyCodeCache.setVerify_code_count(verifyCodeCache.getVerify_code_count() + 1);
        verifyCodeCache.setCreate_time(System.currentTimeMillis());
        updateVerifyCodeCache(verify_token, verifyCodeCache);
        return verify_code;
    }


    /**
     * 校验短信缓存是否有效
     * 是否超时
     * */
    public static boolean isTimeOut(String verify_token){
        VerifyCodeCache verifyCodeCache = getVerifyCodeCache(verify_token);
        if (verifyCodeCache == null) return true;
        long verify_time = (System.currentTimeMillis() - verifyCodeCache.getCreate_time()) / SecondToMilliSecond;
        if (verify_time > ConfigValues.getMobileVerifyCodeTimeout()){
            return true;
        }
        return false;
    }

    /**
     * 校验是否达到每天申请上限
     * */
    public static boolean isDayOu(String verify_token){
        VerifyCodeCache verifyCodeCache = getVerifyCodeCache(verify_token);
        if (verifyCodeCache == null) return false;
        if (verifyCodeCache.getVerify_code_count() > ConfigValues.getVerifyCodeTimesInOutTime()){
            return true;
        }
        return false;
    }


    /**
     * 校验短信缓存是否已存在
     * */
    public static boolean isExist(String verify_token){
        CacheHelper cacheHelper = CacheHelper.getInstance();
        return cacheHelper.exists(verify_token);
    }

    /**
     * 校验短信缓存是否已存在1分钟请求
     * */
    public static boolean isRetry(String verify_token){
        CacheHelper cacheHelper = CacheHelper.getInstance();
        if (cacheHelper.exists(verify_token)){
            VerifyCodeCache verifyCodeCache = getVerifyCodeCache(verify_token);
            long curr_time = System.currentTimeMillis();
            long remaining_time = curr_time - verifyCodeCache.getCreate_time();
            if (remaining_time > (ConfigValues.getSmsSendCountDown() * SecondToMilliSecond)){
                return true;
            }
        }
        return false;
    }

    /**
     * 校验验证码是否正确
     * */
    public static boolean isRight(String verify_token, String verify_code){
        VerifyCodeCache verifyCodeCache = getVerifyCodeCache(verify_token);
        if (verifyCodeCache == null) return false;
        if (verify_code.equals(verifyCodeCache.getVerify_code())){
            return true;
        }
        return false;
    }

    /**
     *  在一分钟内重复请求时
     *  获取验证码倒计时 倒计时 = 系统设置倒计时 - (当前时间 - 验证码创建时间)
     * */
    public static int getCountDown(String verify_token){
        VerifyCodeCache verifyCodeCache = getVerifyCodeCache(verify_token);
        if (verifyCodeCache == null) return (ConfigValues.getSmsSendCountDown());
        long passed_time = System.currentTimeMillis() - verifyCodeCache.getCreate_time();
        int count_down = (int)(ConfigValues.getSmsSendCountDown() - (passed_time/SecondToMilliSecond));
        return count_down;
    }

    /**
     * verify_code获取
     * */
     public static String getVerifyCode(){
         String verify_code = "";
//         if (ConfigValues.getVerifyCodeSuperSwitch()){
//             verify_code = ConfigValues.getVerifyCodeSuperValue();
//         }
//         else {
             verify_code = RandomUtil.genNumericRandomString(ConfigValues.getVerifyCodeLength());
//         }
         return verify_code;
     }

    /**
     * 执行缓存刷新操作
     * */
    private static void updateVerifyCodeCache(String verify_token, VerifyCodeCache verifyCodeCache){
        CacheHelper cacheHelper = CacheHelper.getInstance();
        cacheHelper.set(verify_token, verifyCodeCache.toStringInfo(), ConfigValues.getMobileVerifyCodeTimeout());
    }

    /**
     * 管理端登录缓存 user_token = admin + user_name + password + company_id + sendercomp_id
     * */
    public static String getAdminUserToken(){
        return "";
    }

}
